﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL.Constants;
using BuXiaChu.OrderDishes.BLL.ModelExtensions;
using BuXiaChu.OrderDishes.BLL.StaticCache;

namespace BuXiaChu.OrderDishes.BLL
{
    public class CodeDictionary
    {
        public static CodeDictionary Instance
        {
            get;
            private set;
        }

        static CodeDictionary()
        {
            Instance = new CodeDictionary();
        }

        public static List<ShareCodeModel> GetShareCodeListFromCache()
        {
            #region
            List<ShareCodeModel> list = new List<ShareCodeModel>();
            if (!StaticCacheData.Intance.TryGet<List<ShareCodeModel>>("_ShareCodeListCache_", out list))
            {
                list = ShareCodeManager.GetShareCodeList();
                StaticCacheData.Intance.Set("_ShareCodeListCache_", list);
            }

            return list;
            #endregion
        }

        public static bool ResetShareCodeToCache()
        {
            #region
            StaticCacheData.Intance.DeleteAllCache();
            return true;
            #endregion
        }

        public List<AreaModel> GetAllArea()
        {
            #region
            List<AreaModel> list = new List<AreaModel>();
            if (!StaticCacheData.Intance.TryGet<List<AreaModel>>("_AreaListCache_", out list))
            {
                list = AreaManager.GetAllList();
                StaticCacheData.Intance.Set("_AreaListCache_", list);
            }

            return list;
            #endregion
        }

        /// <summary>
        /// 权限列表
        /// </summary>
        public List<RolesModel> GetRoles()
        {
            #region
            List<RolesModel> list = new List<RolesModel>();
            if (!StaticCacheData.Intance.TryGet<List<RolesModel>>("_RoleListCache_", out list))
            {
                list = RolesManager.GetAll();
                StaticCacheData.Intance.Set("_RoleListCache_", list);
            }

            return list;
            #endregion
        }

        /// <summary>
        /// 应用程序列表
        /// </summary>
        public List<ApplicationsModel> GetApplications()
        {
            #region
            List<ApplicationsModel> list = new List<ApplicationsModel>();
            if (!StaticCacheData.Intance.TryGet<List<ApplicationsModel>>("_ApplicationsListCache_", out list))
            {
                list = ApplicationsManager.GetAll();
                StaticCacheData.Intance.Set("_ApplicationsListCache_", list);
            }

            return list;
            #endregion
        }

        public string GetAreaName(int id)
        {
            #region
            var result = from item in GetAllArea()
                         where item.Id == id
                         select item.Name;
            return result.Count() > 0 ? result.First<string>() : "";
            #endregion
        }

        private Dictionary<long, string> GetShareCodeList(long classID, bool addEmpty)
        {
            #region
            var list = GetListByClassID(classID);
            Dictionary<long, string> result = new Dictionary<long, string>();
            if (addEmpty)
            {
                result.Add(-1, "无");
            }
            foreach (var item in list)
            {
                result.Add(item.CodeID, item.Name);
            }

            return result;
            #endregion
        }

        public List<ShareCodeModel> GetListByClassID(long classID)
        {
            #region
            List<ShareCodeModel> list = GetShareCodeListFromCache();
            List<ShareCodeModel> result = new List<ShareCodeModel>();
            foreach (var item in list)
            {
                if (item.ClassID == classID)
                {
                    var model = item.Clone();
                    result.Add(model);
                }
            }
            return result;
            #endregion
        }

        public string GetCodeName(long codeID)
        {
            #region
            List<ShareCodeModel> list = GetShareCodeListFromCache();
            foreach (var item in list)
            {
                if (item.CodeID == codeID)
                {
                    return item.Name;
                }
            }
            return string.Empty;
            #endregion
        }
    }
}
